package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws Exception{
        //1:连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");//主机ip
        f.setPort(5672);//可选,5672是默认端口
        f.setUsername("admin");//用户名
        f.setPassword("admin");//密码
        f.setVirtualHost("张飞");//虚拟主机
        Connection con = f.newConnection();
        Channel c = con.createChannel();
        //2:定义队列
        c.queueDeclare("helloworld",false,false,false,null);
        System.out.println("等待接收数据");

        //收到消息后用来处理消息的回调对象
        DeliverCallback callback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                String msg = new String(message.getBody(), "UTF-8");
                System.out.println("收到: "+msg);
            }
        };

        //消费者取消时的回调对象
        CancelCallback cancel = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };
        //3:接收消息
        c.basicConsume("helloworld", true, callback, cancel);
    }

}
